from struct import *
import traceback
import hashlib
import socket
import sys
import re
import os
import pylab
import swsw


def tostring(data):
    dtype=type(data).__name__
    if dtype=='ndarray':
        if pylab.shape(data)!=(): data=list(data)
        else: data='"'+data.tostring()+'"'
    elif dtype=='NoneType':
        data=''
    return str(data)


def evaluate(req):
    try:
        res=eval(req)
        lreq=req
        
        li=req.find('(')
        li=li if li>=0 else None
    
        res=req[:li]+'('+swsw.tostring(res)+');'
    except Exception as e:
        print traceback.format_exc()
        res='sys.exception("'+str(e)+'")'

    return res


if __name__=='__main__':
    swsw.handle(swsw.connect()[0], callback=evaluate)
